package com.teapos.service;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.teapos.dao.OrderDao;
import com.teapos.enumtype.TradeTypeEnum;
import com.teapos.util.Tool;

@SuppressWarnings({"rawtypes", "unchecked"})
@Service
public class PrintService {

	@Autowired
	private OrderDao orderDao;
	
	public List listOrders(Map map) {
		Tool.initPage(map);
		String orderNo = Tool.toString(map.get("orderNo"));
		if(StringUtils.isNotBlank(orderNo)) map.put("orderNo", "%"+orderNo+"%");
		List list = orderDao.findOrdersWithCustomer(map);
		Iterator iter = list.iterator();
		while(iter.hasNext()) {
			Map order = (Map) iter.next();
			order.put("type_display", TradeTypeEnum.getEnumByCode(Tool.toInt(order.get("type"))).getDesc());
			order.put("order_date_display", Tool.formatDate((Date)order.get("order_date"), "yyyy-MM-dd HH:mm"));
		}
		return list;
	}

	public int getCount(Map map) {
		String orderNo = Tool.toString(map.get("orderNo"));
		if(StringUtils.isNotBlank(orderNo)) map.put("orderNo", "%"+orderNo+"%");
		return orderDao.findOrdersWithCustomerCount(map);
	}
	
}
